package com.wtwd.device.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.device.model.vo.PageDataVO;
import com.wtwd.device.model.vo.ResultVO;
import com.wtwd.device.constant.RespCode;
import com.wtwd.device.model.entity.HealthType;
import com.wtwd.device.model.entity.HealthTypeAddDto;
import com.wtwd.device.model.vo.HealthTypeVo;
import com.wtwd.device.service.HealthTypeContentService;
import com.wtwd.device.service.HealthTypeService;
import com.wtwd.device.service.RedisService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mjy
 * @since 2023-08-16
 */
@Api(tags = "健康类型管理")
@RestController
@RequestMapping("/health-type")
public class HealthTypeController {


    @Resource
    private HealthTypeService healthTypeService;

    @Resource
    private RedisService redisService;

    /**
     * 分页查询健康类型
     * 暂定根据名称全模糊查询
     *
     * @param pageNo
     * @param pageSize
     * @param name
     * @return
     */
    @ApiOperation(value = "分页查询健康类型", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页数", required = true, dataTypeClass = Integer.class)
            , @ApiImplicitParam(name = "pageSize", value = "页大小", required = true, dataTypeClass = Integer.class)
            , @ApiImplicitParam(name = "name", value = "类型名称", dataType = "String", required = false)
    })
    @GetMapping("/queryPage")
    public ResultVO<PageDataVO<HealthTypeVo>> queryPage(@RequestParam(defaultValue = "1") Integer pageNo
            , @RequestParam(defaultValue = "10") Integer pageSize
            , @RequestParam(value = "name", required = false) String name) {
        Page<HealthType> page = new Page<>(pageNo, pageSize);
        HealthType healthType = new HealthType();
        healthType.setName(name);
        return ResultVO.success(RespCode.SUCCESS, healthTypeService.queryPage(page, healthType));
    }

    /**
     * 添加健康类型
     *
     * @param healthTypeAddDto
     * @return
     */
    @ApiOperation("添加健康类型")
    @ApiResponses({@ApiResponse(code = 8000, message = "该类型已经存在")})
    @PostMapping("/add")
    public ResultVO add(@RequestBody HealthTypeAddDto healthTypeAddDto) {
        if (healthTypeAddDto == null || StringUtils.isBlank(healthTypeAddDto.getName())) {
            return ResultVO.error(RespCode.NO_PARAM);
        }
        if (healthTypeService.authNameIsExist(false, healthTypeAddDto.getName(), null)) {
            return ResultVO.error(RespCode.HEALTH_TYPE_NAME);
        }
        HealthType healthType = new HealthType();
        healthType.setName(healthTypeAddDto.getName());
        healthType.setPath(healthTypeAddDto.getPath());
        healthType.setImage(healthTypeAddDto.getImage());
        boolean rest = false;
        try {
            rest = healthTypeService.insert(healthType);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (rest) {
                return ResultVO.success(RespCode.SUCCESS);
            } else {
                return ResultVO.error(RespCode.FAILED);
            }
        }
    }


    /**
     * 修改健康类型
     *
     * @param healthType
     * @return
     */
    @ApiOperation("修改健康类型")
    @ApiResponses({@ApiResponse(code = 8000, message = "该类型已经存在")})
    @PutMapping("/update")
    public ResultVO update(@RequestBody HealthType healthType) {
        if (healthType == null || StringUtils.isBlank(healthType.getName()) || healthType.getId() == null) {
            return ResultVO.error(RespCode.NO_PARAM);
        }
        if (healthTypeService.authNameIsExist(true, healthType.getName(),healthType.getId())) {
            return ResultVO.error(RespCode.HEALTH_TYPE_NAME);
        }
        boolean b = healthTypeService.updateHealthTypeById(healthType);
        return b ? ResultVO.success(RespCode.SUCCESS) : ResultVO.error(RespCode.FAILED);
    }


    /**
     * 删除健康类型
     * <p>
     * 删除健康类型的同时删除其健康类型的内容
     *
     * @param id
     * @return
     */
    @ApiOperation("删除健康类型")
    @ApiImplicitParam(name = "id", value = "健康类型主键id", required = true, dataTypeClass = Integer.class, paramType = "path")
    @DeleteMapping("/deleteById/{id}")
    public ResultVO deleteById(@PathVariable("id") Integer id) {
        boolean rest = healthTypeService.deleteById(id);
        if (rest) {
            return ResultVO.success(RespCode.SUCCESS);
        } else {
            return ResultVO.error(RespCode.FAILED);
        }
    }

    /**
     * 获取缓存命令集
     */
    @ApiOperation(value = "获取缓存命令集", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "1 type 2:content", dataType = "int", required = false)
    })
    @GetMapping("/getCache")
    public ResultVO getCache(@RequestParam("type") Integer type){
        Map<String, Object> cache=null;
        if (type==1){
           cache = redisService.getHashValue(HealthTypeService.HEATH_TYPE_KEY_PREFIX);
        }else if (type==2){
             cache = redisService.getHashValue(HealthTypeContentService.HEATH_CONTENT_KEY_PREFIX);
        }
        return ResultVO.success(cache);
    }



}

